<?php
/* 采购单 */
class Sourcing_infoModel extends BaseModel
{
    var $table  = 'sourcing';			//表名
    var $prikey = 'sourcing_id';		//主键
    var $_name  = 'sourcing';			//查询用表名
    
    var $_relation = array(
    		'belongs_sample' => array(
				'model' => 'sample',
				'type' => BELONGS_TO,
				'foreign_key' => 'sample_id',
	            'reverse'       => 'has_sourcing_info'
			),
    		'belongs_order_sour' => array(
    				'model' => 'order',
    				'type' => BELONGS_TO,
    				'foreign_key' => 'order_id',
    				'reverse'       => 'has_sourcing'
    		)
    	);
    
    function get_sourcing_list($conditions,$order,$limit){
    	$member_mod    =& m('member');
    	$brand_mod =& m('brand');
    	$sample_mod=& m('sample');
    	 
    	$fields = "sample.sample_id,sample.sample_sn,sample.ref_sample_sn,brand.brand_name,member.user_name,mem.user_name as member_name,sample.factory_price,sample.order_sn,sample.status,sourcing_sn,sourcing_status,sourcing.add_time,if_print,sample.front_pic,sample.front_pic_small,sourcing.sourcing_id,amount";
    	$tables = "{$this->table} sourcing " .
    	"LEFT JOIN {$sample_mod->table} sample ON sample.sample_id=sourcing.sample_id " .
    	"LEFT JOIN {$brand_mod->table} brand ON brand.brand_id = sample.brand_id " .
    	"LEFT JOIN {$member_mod->table} member ON member.user_id = sample.merchandiser_id " .
    	"LEFT JOIN {$member_mod->table} mem ON mem.user_id = sample.buyer_id ";
    	 
    	/* 条件(WHERE) */
    	$conditions = $this->_getConditions($conditions, true);
    	/* 排序(ORDER BY) */
    	if ($order)
    	{
    		$order = ' ORDER BY ' . $this->getRealFields($order);
    	}
    	 
    	/* 分页(LIMIT) */
    	$limit && $limit = ' LIMIT ' . $limit;
    	if ($limit)
    	{
    		$this->_updateLastQueryCount("SELECT COUNT(*) as c FROM {$tables}{$conditions}");
    	}
    	 
    	/* 完整的SQL */
    	$sql = "SELECT {$fields} FROM {$tables}{$conditions}{$order}{$limit}";
    	 
    	$sour_list =$this->db->getAll($sql);
    	return $sour_list;
    }
    
    //查询生产板的采购单列表
    function get_sourcing_status_list($conditions,$order,$limit){
    	$order_mod=& m('order');
    	
    	$fields = "sourcing_id,o.order_id,o.order_sn,o.brand_name,o.merchandiser,o.buyer_name,o.factory_name,o.status,sourcing_sn,sourcing_status,sourcing.add_time,if_print,o.factory_price,o.front_pic";
    	$tables = "{$this->table} sourcing " .
    	"LEFT JOIN {$order_mod->table} o ON o.order_id=sourcing.order_id ";
    	
    	/* 条件(WHERE) */
    	$conditions = $this->_getConditions($conditions, true);
    	/* 排序(ORDER BY) */
    	if ($order)
    	{
    		$order = ' ORDER BY ' . $this->getRealFields($order);
    	}
    	
    	/* 分页(LIMIT) */
    	$limit && $limit = ' LIMIT ' . $limit;
    	if ($limit)
    	{
    		$this->_updateLastQueryCount("SELECT COUNT(*) as c FROM {$tables}{$conditions}");
    	}
    	
    	/* 完整的SQL */
    	$sql = "SELECT {$fields} FROM {$tables}{$conditions}{$order}{$limit}";
    	
    	$sour_list =$this->db->getAll($sql);
    	return $sour_list;
    }
}

?>